package com.demo2.admin;


import com.demo2.dao.*;
import com.demo2.entity.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import javax.xml.ws.RequestWrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PropertyValueMapper propertyValueMapper;
    @Autowired
    private ProductImageMapper productImageMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ProductOrderItemMapper productOrderItemMapper;

    @GetMapping("/list/{pId}")
    @ResponseBody
    public Product  save(@PathVariable Integer pId){
        System.out.println("user save ...");
        Product product=productMapper.getById(pId);
        System.out.println(product);
        Integer id=product.getProduct_id();
//        spshuxin
        List<PropertyValue> propertyValueItem=propertyValueMapper.selectPropertyValueId(id);
        System.out.println(propertyValueItem);
//
        product.setPropertyValueList(propertyValueItem);
//        sptupian
        List<ProductImage> productImage=productImageMapper.GetProductImage(id);
        System.out.println(productImage);
        product.setSingleProductImageList(productImage);
        List<Review> reviewList= reviewMapper.getReview(id);
//        Integer productimageProductId=product.product_p
        System.out.println(reviewList);
        product.setReviewList(reviewList);

        List<ProductOrderItem> productOrderItemList=productOrderItemMapper.getProductOrderItem(id);
        System.out.println(productOrderItemList);
        product.setProductOrderItemList(productOrderItemList);
        List<ProductImage> productDetailImageList=productImageMapper.GetProductProductImage(id);
        product.setDetailProductImageList(productDetailImageList);
        return product;
    }


    @PostMapping("/list/add")
    @ResponseBody
    public Integer addProduct(@RequestBody Product product){
//        Integer data=productMapper.insertOne(product);
        Integer data=productMapper.add(product);

        return 1;
    }
    @PostMapping("/list/update")
    @ResponseBody
    public Integer updateProduct(@RequestBody Product product){
//        Integer data=productMapper.insertOne(product);
        Integer data=productMapper.updateUser(product);

        return data;
    }

    @PostMapping("/list/deleteList")
    @ResponseBody
    public Integer deleteListProduct(@RequestBody Map<String,ArrayList<Integer>> map){
//        Integer data=productMapper.insertOne(product);
        ArrayList<Integer> list =map.get("idList");
        System.out.println(list);
        Integer data=productMapper.deleteList(list);
        return data;
    }
}
